package com.mikey.springcloud.controller;

import com.mikey.springcloud.pojo.Category;
import com.mikey.springcloud.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :Mikey
 * @description :
 * @create :2022-08-29 19:08:00
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //获得服务信息
    @Autowired
    private DiscoveryClient discovery;

    @PostMapping("/add")
    public int add(@RequestBody Category category) {
        return categoryService.insert(category);
    }

    @PostMapping("/delete/{id}")
    public int delete(@PathVariable Integer id) {
        return categoryService.delete(id);
    }

    @PostMapping("/update")
    public int update(@RequestBody Category category) {
        return categoryService.update(category);
    }

    @GetMapping("/select/{id}")
    public Category selectOne(@PathVariable("id") Integer id) {
        System.out.println(id);
        return categoryService.selectOne(id);
    }

    @GetMapping("/list")
    public List<Category> list() {
        return categoryService.selectAll();
    }

    @RequestMapping("/discovery")
    public Object discovery() {
        List<String> services = discovery.getServices();
        System.out.println("discovery=>service:" + services);
        //获得服务实体
        List<ServiceInstance> instances = discovery.getInstances("SPRINGCLOUD-PROVIDER-CATEGORY");
        for (ServiceInstance instance : instances) {
            // 打印服务实体信息
            System.out.println(instance.getHost() + instance.getPort() + instance.getUri() + instance.getServiceId());
        }
        return this.discovery;
    }
}